﻿/**********************************************
系统:Ebada物流企业ERP
模块:单证种类管理
作者:Rabbit
创建时间:2011-3-25
最后一次修改:2011-3-25
***********************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Components;
using Ebada.Client;
using Ebada.Platform.Model;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList;

namespace Ebada.Modules.Document
{
    /// <summary>
    /// 单证种类管理
    /// </summary>
    public partial class UCDocumentTypeTree : DevExpress.XtraEditors.XtraUserControl {

        TreeViewOperation<DocumentType> treeViewOperator;
        private string act_code = "";

        public string Act_code
        {
            get { return act_code; }
            set { act_code = value; }
        }

        public TreeViewOperation<DocumentType> TreeViewOperator {
            get { return treeViewOperator; }
            set { treeViewOperator = value; }
        }
        public event SendDataEventHandler<DocumentType> FocusedNodeChanged;
        public event SendDataEventHandler<DocumentType> AfterAdd;
        public event SendDataEventHandler<DocumentType> AfterEdit;
        public event SendDataEventHandler<DocumentType> AfterDelete;
        public UCDocumentTypeTree() {
            InitializeComponent();
            treeViewOperator = new TreeViewOperation<DocumentType>(treeList1,barManager1,new frmDocumentTypeEdit());
            treeViewOperator.CreatingObjectEvent += treeViewOperator_CreatingObject;
            treeViewOperator.AfterAdd += treeViewOperator_AfterAdd;
            treeViewOperator.BeforeInsert += treeViewOperator_BeforeInsert;
            treeViewOperator.BeforeUpdate += treeViewOperator_BeforeUpdate;
            treeViewOperator.AfterEdit +=treeViewOperator_AfterEdit;
            treeViewOperator.AfterDelete +=treeViewOperator_AfterDelete;
            treeList1.FocusedNodeChanged += treeList1_FocusedNodeChanged;
            initColumns();
        }

        void treeViewOperator_BeforeUpdate(object render, ObjectOperationEventArgs<DocumentType> e)
        {//记录修改保存前发生
            e.Value.LastEditTime = DateTime.Now;
        }

        void treeViewOperator_BeforeInsert(object render, ObjectOperationEventArgs<DocumentType> e)
        {//新记录保存前发生
            e.Value.CreatedDate = DateTime.Now;
        }

        void treeViewOperator_AfterDelete(DocumentType newobj) {
            if (AfterDelete != null)
                AfterDelete(treeList1, newobj);
        }

        void treeViewOperator_AfterEdit(DocumentType newobj) {
            if (AfterAdd != null)
                AfterEdit(treeList1, newobj);
        }

        void treeViewOperator_AfterAdd(DocumentType newobj) {
            if (AfterAdd != null)
                AfterAdd(treeList1, newobj);
        }

        void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e) {
            if (FocusedNodeChanged != null)
                FocusedNodeChanged(treeList1,treeList1.GetDataRecordByNode(e.Node) as DocumentType);
        }

        void treeViewOperator_CreatingObject(DocumentType newobj) {
        }
        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
            treeList1.KeyFieldName = "DocumentTypeID";
            treeList1.ParentFieldName = "ParentID";
            if (this.Site == null)
            {
                if (Act_code == "")
                {
                    InitData();
                }
                else
                {
                    InitData(Act_code);
                }
            }
        }
        void initColumns()
        {

            treeList1.Columns[9].Caption = "创建人ID";
            treeList1.Columns[12].Caption = "修改人ID";

        }
        /// <summary>
        /// 初始化数据
        /// </summary>
        public void InitData() {
            treeViewOperator.RefreshData("order by parentid,DocumentTypeID");    
        }
        public void InitData(string _act_code)
        {
            treeViewOperator.RefreshData(" where Act_Code='" + _act_code + "'");
        }
        public void InitData2(string _name)
        {
            treeViewOperator.RefreshData(" where DocTypeName like '%" + _name + "%'");
        }
        private void btFind2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmDocumentTypeFind frm = new frmDocumentTypeFind();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                InitData2(frm.StrName);
            }
        }
    }
}
